package com.leetcode.LC_MED;

public class LC1049 {
    public int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for (int stone : stones) {
            sum += stone;
        }
        int weight = sum / 2;
        int[] dp = new int[weight + 1];
        for (int i = 0; i < stones.length; i++) {
            for (int j = weight; j >= stones[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        return Math.abs((sum - dp[weight]) - dp[weight]);
    }
}
